<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 */
 
$team = new team;
$updatecontent = true;
if ($CURRENT_USER->groupid == GROUP_ADMIN) {
	$team->status=STATUS_ACTIVE;
	$teamlist = $loader->loadListBy($team,$team->listByStatus());	
} else {
	$team = new team;
	$team->id = $CURRENT_USER->teamid;
	$loader->get($team);
	$teamlist = array($team);
	$teamobj=$team;
}

if ($data['fteamid']!='') {
	foreach ($teamlist as $key => $obj) {
		if ($obj->id == $data['fteamid']) {
			$teamobj=$obj;	
		}	
	}
}

if ($data['op']=='act' or $data['op']=='deact') {

	foreach($data['userid'] as $id) {
		$us = new user;
		$us->userid=$id;
		$ret = $loader->get($us);
		if ($ret) {
			if ($data['op']=='act') {
				$us->status=STATUS_ACTIVE;
			} else {
				$us->status=STATUS_INACTIVE;
			}
			$loader->update($us);
			sendUserStatusMail($us);
			$message=$LANG['userupdated'];
		} else {
			$error=$LANG['updateerr'];
			$updatecontent=false;
		}
	}

} elseif ($data['op']=='chgrp') {
	$updatecontent=false;
	$us = new user;
	$us->userid=$data['id'];
	$ret = $loader->get($us);
	if ($ret) {
		$us->groupid=$data['newvalue'];
		$loader->update($us);
		$message=$LANG['userupdated'];
	} else {
		$error=$LANG['updateerr'];	
	}
} elseif ($data['op']=='chteam') {
	$updatecontent=false;
	$us = new user;
	$us->userid=$data['id'];
	$ret = $loader->get($us);
	if ($ret) {
		$us->teamid=$data['newvalue'];
		$loader->update($us);
		
		$team = new team;
		$team->id=$us->teamid;
		$loader->get($team);
		$message=$LANG['userupdated'];
		switchTeamMail($us);
	} else {
		$error=$LANG['updateerr'];	
	}
} elseif ($data['op']=='reset' and $CURRENT_USER->groupid == GROUP_ADMIN) {
	$updatecontent=false;
	foreach($data['userid'] as $id) {
		$us = new user;
		$us->userid=$id;
		$ret = $loader->get($us);
		if ($ret) {
			$newpass = $us->userid;
			$us->password=$newpass;
			$loader->update($us);
			$message=$LANG['userupdated'];
		} else {
			$error=$LANG['updateerr'];	
		}
	}
	
} elseif ($data['op']=="remove" and $CURRENT_USER->groupid == GROUP_ADMIN) {
	foreach($data['userid'] as $id) {
		$us = new user;
		$us->userid=$id;
		$loader->delete($us);
	}
	$message=$LANG['userupdated'];
}

if ($updatecontent) {
	$us = new user;
	$us->groupid=$data['fgroupid'];
	if ($CURRENT_USER->groupid == GROUP_ADMIN) {
		$us->teamid=$data['fteamid'];	
	} else {
		$us->teamid=$CURRENT_USER->teamid;
	}
	$us->status=$data['fstatusid'];
	$list = $loader->loadListBy($us,$us->listByFilters());
	
	foreach ($list as $key => $obj) {
		foreach ($teamlist as $tkey => $tobj) {
			if ($obj->teamid == $tobj->id) {
				$obj->team=$tobj;	
			}	
		}
	}
	
	$tm->assign('groupid',$us->groupid);
	$tm->assign('teamid',$us->teamid);
	$tm->assign('statusid',$us->status);
	
	$tm->assign('statuslist',$STATUS_LIST);
	$tm->assign('grouplist',$GROUP_LIST);
	$tm->assign('teamlist',$teamlist);
	$tm->assign('team',$teamobj);
	
	$tm->assign('list',$list);
}

?>
